var addrole = {
	
	id:null,
	initPage:function(){
		addrole.initLoad(addrole.id);
		$("#cancelBtn").click(function(){
			addrole.backList();
		});
		$("#confirmBtn").click(function(){
			addrole.saveOrupdate();
		});
	},
	
	initLoad : function(id) {
		if(id){
			rolemanager.queryList({"id":id},function(data){
				if(data.data && data.retCode == 0){
					var obj= data.data[0];
					$("#name").val(obj.name);
					var powers = obj.power.split('');
					$("input:checkbox").each(function(key){
		                if(powers[key] == "1"){
		                	$(this).prop("checked","checked");
		                }
		            });
				}
			});
		}
	},
	
	saveOrupdate:function(){
		var name = $("#name").val();
		var remark = $("#remark").val();
		if( name=='' ){
			util.alertMessage("请输入数据");
		}else{
			var power = "";
			$("input:checkbox").each(function(key){
                if($(this).is(':checked')){
                	power += "1";
                }else{
                	power += "0";
                }
            });
			var params ={
					'name':name,
					'power':power
			};
			if(addrole.id){
				params['id'] = addrole.id;
			}
			
			rolemanager.save(params,addrole.add_successcb,addrole.add_successcb);
		}
	},
	
	add_successcb:function(data){
		 if(data.retCode == 0){
			 layer.msg('操作成功', {icon: 1});
			 addrole.backList();
		 }else{
			 util.alertMessage(data.message || '操作失败');
		 }
	},
		
	backList:function(){
		util.loadContent(util.webpath+'role/roleList.html','contentDiv',{},function(){});
	},
}
